﻿#pragma once
#include <iostream>
//#include <utility>
using namespace std;

enum Colour {
	RED,
	BLACK
};

//BRTree树结构
template<class T>
struct RBTreeNode
{
	// 这⾥更新控制平衡也要加⼊parent指针
	T _kv;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;
	RBTreeNode(const T& kv = T())
		: _kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}
};

template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> Self;

	Node* _node;
	Node* _root;


	RBTreeIterator(Node* node, Node* root)
		:_node(node)
		,_root(root)
	{}

	Self operator++()
	{
		if (_node->_right)
		{
			// 右不为空，中序下一个访问的节点是右子树的最左(最小)节点
			Node* min = _node->_right;
			while (min->_left)
			{
				min = min->_left;
			}

			_node = min;
		}
		else
		{
			// 右为空，祖先里面孩子是父亲左的那个祖先
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}

		return *this;
	}

	Self operator--()
	{
		if (_node == nullptr)  // --end()
		{
			// --end()，特殊处理，走到中序最后一个结点，整棵树的最右结点
			Node* rightMost = _root;
			while (rightMost && rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else if (_node->_left)
		{
			// 左子树不为空，中序左子树最后一个
			Node* rightMost = _node->_left;
			while (rightMost->_right)
			{
				rightMost = rightMost->_right;
			}
			_node = rightMost;
		}
		else
		{
			// 孩子是父亲右的那个祖先
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	Ref operator*()
	{
		return _node->_kv;
	}

	Ptr operator->()
	{
		return &_node->_kv;
	}

	bool operator!= (const Self& s) const
	{
		return _node != s._node;
	}

	bool operator== (const Self& s) const
	{
		return _node == s._node;
	}
};

// 请模拟实现红黑树的插入--注意：为了后序封装map和set，本文在实现时给红黑树多增加了一个头结点
template<class k, class t, class KeyOfT>
class RBTree
{
	typedef RBTreeNode<t> Node;
public:
	KeyOfT Kot;

	RBTree()
		:_pHead(nullptr)
		, _treecount(0)
	{
		//_pHead->_left = nullptr;
		//_pHead->_right = nullptr;
	}

	typedef RBTreeIterator<t, t&, t*> Iterator;
	typedef RBTreeIterator<t, const t&, const t*> ConstIterator;

	Iterator Begin()
	{
		Node* cur = _pHead;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return Iterator(cur, _pHead);
	}

	Iterator End()
	{
		return Iterator(nullptr, _pHead);
	}

	ConstIterator Begin() const
	{
		Node* cur = _pHead;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return ConstIterator(cur, _pHead);
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr, _pHead);
	}

	// 注意：为了简单起见，本次实现红黑树不存储重复性元素
	pair<Iterator, bool> Insert(const t& V)
	{
		if (_pHead == nullptr)
		{
			_pHead = new Node(V);
			_pHead->_col = BLACK;
			_treecount++;
			return { Iterator(_pHead, _pHead), true };
		}
		Node* parent = nullptr;
		Node* cur = _pHead;
		while (cur)
		{
			if (Kot(cur->_kv) < Kot(V))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (Kot(cur->_kv) > Kot(V))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return { Iterator(cur, _pHead), false };
			}
			/*if (cur == _pHead)
			{
				break;
			}*/
		}
		cur = new Node(V);
		Node* he = cur;
		cur->_col = RED;
		if (Kot(parent->_kv) < Kot(V))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;
		//对RBTree进行调整
		Node* u = nullptr;
		//Node* g = parent->_parent;
		while (parent && parent->_col == RED)
		{
			Node* g = parent->_parent;
			if (g->_left == parent)
			{
				u = g->_right;
				//1.单变色
				if (u && u->_col == RED)
				{
					u->_col = BLACK;
					parent->_col = BLACK;
					g->_col = RED;

					cur = g;
					parent = cur->_parent;
					//g = parent->_parent;
				}
				else
				{
					//2.变色+单旋
					// u 在 g 的右进行有单旋
					if (cur == parent->_left)
					{
						/*g->_left = parent->_right;
						if (parent->_right)
							parent->_right->_parent = g;
						parent->_right = g;
						parent->_parent = g->_parent;
						g->_parent = parent;*/
						RotateR(g);
						parent->_col = BLACK;
						g->_col = RED;
					}
					//3.双转
					else
					{
						RotateL(parent);
						RotateR(g);
						cur->_col = BLACK;
						g->_col = RED;
					}
					break;
				}
			}
			else if (g->_right == parent)
			{
				u = g->_left;
				if (u && u->_col == RED)
				{
					u->_col = BLACK;
					parent->_col = BLACK;
					g->_col = RED;
					cur = g;
					parent = cur->_parent;
					//g = parent->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						RotateL(g);
						parent->_col = BLACK;
						g->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(g);
						cur->_col = BLACK;
						g->_col = RED;
					}
					break;
				}
			}
			_pHead->_col = BLACK;
		}
		_treecount++;
		return { Iterator(he, _pHead), false };
	};
	// 检测红黑树中是否存在值为data的节点，存在返回该节点的地址，否则返回nullptr
	Node* Find(const k& key)
	{
		Node* data = _pHead;
		while (data)
		{
			if (Kot(data->_kv) == key)
				return _pHead;
			if (Kot(data->_kv) < key)
			{
				data = data->_right;
			}
			else if (Kot(data->_kv) > key)
			{
				data = data->_left;
			}
		}
		return nullptr;
	}

	// 获取红黑树最左侧节点
	Node* LeftMost()
	{
		return _leftMost(_pHead);
	}

	// 获取红黑树最右侧节点
	Node* RightMost()
	{
		Node* x = _pHead;
		while (x && x->_right != nullptr)
		{
			x = x->_right;
		}
		return x;
	}
	void InOrder()
	{
		_InOrder(_pHead);
		cout << endl;
	}

	int Height()
	{
		return _Height(_pHead);
	}

	int Size()
	{
		return _Size(_pHead);
	}

	
private:
	
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;

		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	// 左单旋
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;
		if (parentParent == nullptr)
		{
			_pHead = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}
	// 右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* pParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _pHead)
		{
			_pHead = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}

			subL->_parent = pParent;
		}
	}
	// 为了操作树简单起见：获取根节点
	Node*& GetRoot()
	{
		return _pHead;
	}

	Node* _leftMost(Node* node)
	{
		if (node == nullptr || node->_left == nullptr)
		{
			return node;
		}
		_leftMost(node->_left);
	}
	private:
		int _treecount;
		Node* _pHead;
};
